import { ElMessage, type FormInstance } from "element-plus";
import { addProduct } from "@/api/shop/index";
import { transformToSubmit } from "./utils";
import router from "@/router";
import { form, route } from "./add.vue";

export const submitForm = async (formEl: FormInstance | undefined) => {
if (!formEl) return;
await formEl.validate((valid, fields) => {
if (valid) {
const data = transformToSubmit(form);
if (route.query.spuId) {
// updateProduct
} else {
addProduct(data).then(() => {
ElMessage.success("提交成功");
setTimeout(() => {
router.push("/shop/product");
}, 1000);
});
}
} else {
console.log("error submit!", fields);
}
});
};
